
export { };
// 5.单例和构建分离模式 
  //希望封装变化 createInstance方法可以常见任意的实例
interface Window{
  new(): Window;
}
let Window = function () {
  
};
Window.prototype.hello = function () {
  console.log('hello---4');
};

let createInstance = function (constructor:any) {
  let instance:any;
  return function Any(this:any) {
    if (!instance) {
      console.log('4   没有走缓存');
      //正常来说  此时this.__proto__ = Any.prototype
      constructor.apply(this,arguments);
      Object.setPrototypeOf(this,constructor.prototype);
      instance = this;
    };
    console.log('4  走缓存');
    return instance
  }
};
//4 测试单例
let createWindow = createInstance(Window);
let w1 = new (createWindow as any)();
let w2 =new (createWindow as any)();
console.log(`5 两个实例是否一样${w1 == w2}`)
